package utils;

import android.content.Context;
import android.widget.ImageView;

import com.ygk.bluepath.core.BApp;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

/**
 * @Description 图片获取
 */
public class ImageLoaderUtil {

	private static ImageLoader mImageLoader;
	private static DisplayImageOptions mOptions;
	private static ImageLoaderUtil mImageLoaderUtil;

	public static ImageLoaderUtil getInstance(Context context) {

		if (mImageLoaderUtil == null) {
			mImageLoaderUtil = new ImageLoaderUtil();
			mImageLoader = BApp.getInstance().getmImageLoader();
			initOptions();
		}

		return mImageLoaderUtil;
	}

	private static void initOptions() {
		// TODO Auto-generated method stub
		mOptions = new DisplayImageOptions.Builder()
			/*	.showImageOnFail(R.drawable.read_figure_chart)
				.showImageOnLoading(R.drawable.read_figure_chart)
				.showImageForEmptyUri(R.drawable.read_figure_chart)*/
				.cacheInMemory(true).cacheOnDisk(true)
				//.resetViewBeforeLoading(true)
				//.displayer(new RoundedBitmapDisplayer(20))
				.imageScaleType(ImageScaleType.EXACTLY)
				.displayer(new SimpleBitmapDisplayer()).build();
	}

	private DisplayImageOptions customOptions(int onLoadingPic, int onFailPic,
			int onEmptyPic) {
		mOptions = new DisplayImageOptions.Builder().showImageOnFail(onFailPic)
				.showImageOnLoading(onLoadingPic)
				.showImageForEmptyUri(onEmptyPic).cacheInMemory(true)
				.cacheOnDisk(true)
				//.resetViewBeforeLoading(true)
				//.displayer(new RoundedBitmapDisplayer(20))
				.imageScaleType(ImageScaleType.EXACTLY)
				.displayer(new SimpleBitmapDisplayer()).build();
		return mOptions;
	}

	public void disPlayImage(String url, ImageView img) {
		mImageLoader.displayImage(url, img, mOptions);
	}

	public void disPlayImage(String url, ImageView img, int onLoadingPic,
			int onFailPic, int onEmptyPic) {
		mOptions = customOptions(onLoadingPic, onFailPic, onEmptyPic);
		mImageLoader.displayImage(url, img, mOptions);
	}

	public void disPlayImage(String url, ImageView img, int onLoadingPic,
			int onFailPic, int onEmptyPic, ImageLoadingListener listener) {
		mOptions = customOptions(onLoadingPic, onFailPic, onEmptyPic);
		mImageLoader.displayImage(url, img, mOptions, listener);
	}

	public void disPlayImage(String url, ImageView img,
			ImageLoadingListener listener) {
		mImageLoader.displayImage(url, img, mOptions, listener);
	}
}
